﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.Model;
using ZF.BLL;
using GCELibrary.Current;
using ZF.BLL.Linq;
using ZF.DAL;
using System.Web.UI.HtmlControls;

namespace ZF.WS.info
{
    public partial class Estatelist : BLL.Page.PageBase
    {
        [PageArgument(Key = "ht", DefaultValue=null)]
        public string ht { get; set; }
        [PageArgument(Key = "dt", DefaultValue = -1)]
        public int dt { get; set; }
        [PageArgument(Key = "pi",DefaultValue=0)]
        public int Pi { get; set; }
        [PageArgument(Key = "days", DefaultValue = 0)]
        public int days { get; set; }
        [PageArgument(Key = "pmin", DefaultValue = 0)]
        public int pmin { get; set; }
        [PageArgument(Key = "pmax", DefaultValue = 0)]
        public int pmax { get; set; }
        [PageArgument(Key = "key", DefaultValue=null)]
        public string key { get; set; }

        [PageArgument(Key = "aid1", DefaultValue = 0)]
        public int AreaId1 { get; set; }
        [PageArgument(Key = "aid2", DefaultValue = 0)]
        public int AreaId2 { get; set; }
        protected string _param;

        protected void Page_Load(object sender, EventArgs e)
        {

            var ptlist = DictionaryConfig.HArea
              .Where(p => p.ParentId == 0).Select(p => new SimpleTree<int>()
              {
                  Id = p.AreaId,
                  Name = p.Name,
                  ParentId = 0,
              }).ToList();

            DictionaryConfig.HArea
            .ToList().ForEach(p =>
            {
                if (p.ParentId != 0)
                {
                    var parent = ptlist.FirstOrDefault(pt => pt.Id == p.ParentId);
                    if (parent != null)
                    {
                        parent.Childs.Add(new SimpleTree<int>()
                        {
                            Id = p.AreaId,
                            Name = p.Name,
                            ParentId = p.ParentId,
                        });
                    }
                }
            });
            this.lvPTitle.DataSource = ptlist;
            this.lvPTitle.DataBind();
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                string param = "";
                foreach (var name in Request.QueryString.AllKeys)
                {
                    if (name != null && !name.EqIgnoreCase("pi"))
                    {
                        param += "&" + name + "=" + HttpUtility.UrlEncode(Request.QueryString[name]);
                    }
                }

                _param = "/house/Estatelist.aspx?" + param;

          

                var sel = dc.H_Estate.Where(p =>   p.IsVerify );



                if (AreaId1 > 0)
                    sel = sel.Where(p => p.AreaId1 == AreaId1);
                if (AreaId2 > 0)
                    sel = sel.Where(p => p.AreaId2 == AreaId2);

                if (dt != -1)
                    sel = sel.Where(p => true);

                if (!string.IsNullOrEmpty (ht))
                    sel = sel.Where(p => p.HouseType==ht);
                if (!string.IsNullOrEmpty(key))
                    sel = sel.Where(p => p.Title.Contains(key) );
                if (days > 0)
                    sel = sel.Where(p => p.AddTime > DateTime.Now.AddDays(-days));
                if(pmin>0)
                    sel = sel.Where(p => p.Price >= pmin);
                if (pmax > 0)
                    sel = sel.Where(p => p.Price <= pmax);
            
                var pagesize = 10;
                var rowcount = sel
                   .OrderByDescending(p => p.EstateId).Count();
                var pagecount = rowcount / pagesize;
                if (rowcount % pagesize != 0)
                    pagecount++;

                if (Pi >= pagecount)
                    Pi = pagecount - 1;




                if (rowcount > 0)
                {
                    var tlist = sel
                            .OrderByDescending(p => p.EstateId).Skip(Pi * pagesize)
                            .Take(pagesize).ToList();


                    this.lvList.DataSource = tlist.Select(p => new
                    {
                        House = p,
                        City = SiteInfo.Instance.GetAreaOrDefault(p.AreaId1).Name,
                        Type = p.HouseType,



                    }).ToList();
                    this.lvList.DataBind();
                }

            }
        }


        protected void InitControls()
        {
        }
    }
}
